'use strict';

var fs = require('fs'),
    url = require('url'),
    path = require('path'),
    http = require('http');

var root = path.resolve('.');
console.log('root dir :' + root);
var server=http.createServer(function (request, response) {
    var pathname=url.parse(request,url).pathname
    var filepath = path.join(root, pathname);
    fs.stat(filepath,function (err, stats) {
        if (!err && stats.isFile()) {
            console.log('200' + request.url);
            response.writeHead(200);
            fs.createReadStream(filepath).pipe(response);
        }else {
            console.log('404' + request.url);
            response.writeHead(404);
            response.end("404 not found")
        }
    })
})

server.listen(8888);
console.log("File Server running at 8888.");
